package com.hzeport.bgs22emduty.inbound.api;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzeport.bgs22emduty.config.SystemConfig;
import com.hzeport.bgs22emduty.config.login.LoginCompoentService;
import com.hzeport.bgs22emduty.config.login.UserInfoDetailVO;
import com.hzeport.bgs22emduty.config.systemLog.OperLog;
import com.hzeport.bgs22emduty.dto.experience.*;
import com.hzeport.bgs22emduty.dto.messgeDistribute.ScreenMessageDistributeHideDTO;
import com.hzeport.bgs22emduty.entity.ScreenDutyExperience;
import com.hzeport.bgs22emduty.entity.ScreenDutyMaps;
import com.hzeport.bgs22emduty.enums.HideStatusEnum;
import com.hzeport.bgs22emduty.outbound.ScreenDutyMapsRepository;
import com.hzeport.bgs22emduty.service.ScreenDutyExperienceService;
import com.hzeport.bgs22emduty.auth.adapter.UserService;
import com.hzeport.bgs22emduty.auth.dto.Role;
import com.hzeport.bgs22emduty.auth.h4a.constant.H4AEnum;
import com.hzeport.bgs22emduty.common.base.BaseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * ${comments}
 *
 * @author code generator
 * @date 2023-02-14 09:39:44
 */
@RestController
@AllArgsConstructor
@RequestMapping("/screendutyexperience")
@Api(value = "screendutyexperience", tags = "值班工作经验管理")
@Slf4j
public class ScreenDutyExperienceController {

    private final ScreenDutyExperienceService screenDutyExperienceService;

    private final ScreenDutyMapsRepository screenDutyMapsRepository;

    private final LoginCompoentService loginCompoentService;

    private final SystemConfig systemConfig;

    public final UserService userService;

    @ApiOperation(value = "分页查询", notes = "分页查询")
    @PostMapping("/page")
    public BaseResult< Page<ScreenDutyExperience>> getScreenDutyExperiencePage(@RequestBody ScreenDutyExperienceDTO screenDutyExperience) {
        Page<ScreenDutyExperience>  dutyMessageList=screenDutyExperienceService.getScreenDutyExperiencePage(screenDutyExperience);
        return BaseResult.ok(dutyMessageList);
    }

    @ApiOperation(value = "获取HbUrl", notes = "获取HbUrl")
    @GetMapping("/getHbUrl")
    public BaseResult<String> getHbUrl(@RequestParam("customId") String customId, HttpServletRequest request) {
        UserInfoDetailVO userInfoDetailVO = loginCompoentService.getUserInfo();
        if (userInfoDetailVO.getRole() != null && userInfoDetailVO.getRole().contains("ZGGLY")) {
            if(StringUtils.isNotBlank(systemConfig.getAddUrl())){
                return BaseResult.ok(systemConfig.getAddUrl());
            }else {
                return BaseResult.failed("该海关下未配置新增地址");
            }
        }else {
            ScreenDutyMaps dutyMaps = screenDutyMapsRepository.getOne(new LambdaQueryWrapper<ScreenDutyMaps>()
                    .eq(ScreenDutyMaps::getCustomCode, customId) , false );
            if(StringUtils.isNotBlank(dutyMaps.getAddUrl())){
                return BaseResult.ok(dutyMaps.getAddUrl());
            }else {
                return BaseResult.failed("该关区未配置新增地址");
            }
        }
    }

    /**
     **
     * 通过id查询${comments}
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("getById")
    public BaseResult getById(@RequestParam("id") String id) {
        return BaseResult.ok(screenDutyExperienceService.detail(id));
    }

    /**
     * 新增${comments}
     * @return R
     */
    @ApiOperation(value = "新增${comments}", notes = "新增${comments}")
    //@SysLog(.*)
    @PostMapping("save")
    @OperLog(systemName = "GZJY",description = "GZXZ")
    public BaseResult save(@Valid@RequestBody ScreenDutyExperienceSaveDTO screenDutyExperienceSaveDTO) {
        return BaseResult.ok(screenDutyExperienceService.saveExperiences(screenDutyExperienceSaveDTO));
    }
//
//    /**
//     * 修改${comments}
//     * @param screenDutyExperience ${comments}
//     * @return R
//     */
//    @ApiOperation(value = "修改${comments}", notes = "修改${comments}")
//    //@SysLog(.*)
//    @PostMapping("updateById")
//    public BaseResult updateById(@RequestBody ScreenDutyExperience screenDutyExperience) {
//        return BaseResult.ok(screenDutyExperienceService.updateById(screenDutyExperience));
//    }

    /**
     * 通过id删除${comments}
     * @return R
     */
    @ApiOperation(value = "通过id隐藏", notes = "通过id隐藏")
    //@SysLog(.*)
    @PostMapping("/hide")
    @OperLog(systemName = "GZJY",description = "GZYC")
    public BaseResult hide(@Valid @RequestBody ScreenDutyExperienceHideDTO screenDutyExperienceHideDTO) {
        if (HideStatusEnum.NOT_HIDED.getValue().equals(screenDutyExperienceHideDTO.getStatus())) {
            return BaseResult.ok(screenDutyExperienceService.hide(screenDutyExperienceHideDTO),"取消隐藏成功");
        }else {
            return BaseResult.ok(screenDutyExperienceService.hide(screenDutyExperienceHideDTO),"隐藏成功");
        }
    }

    /**
     * 通过id删除${comments}
     * @return R
     */
    @ApiOperation(value = "通过id删除${comments}", notes = "通过id删除${comments}")
    //@SysLog(.*)
    @PostMapping("/delete")
    @OperLog(systemName = "GZJY",description = "GZSC")
    public BaseResult remove(@Valid @RequestBody ScreenDutyExperienceRemoveDTO screenDutyExperienceRemoveDTO) {
        return BaseResult.ok(screenDutyExperienceService.remove(screenDutyExperienceRemoveDTO));
    }

    /**
     * 通过id删除${comments}
     * @return R
     */
    @ApiOperation(value = "改变发布状态", notes = "改变发布状态")
    //@SysLog(.*)
    @PostMapping("/changeStatus")
    @OperLog(systemName = "GZJY",description = "FBZT")
    public BaseResult changeStatus(@Valid @RequestBody ScreenDutyExperienceSubmitDTO screenDutyExperienceSubmitDTO) {
        return BaseResult.ok(screenDutyExperienceService.changeStatus(screenDutyExperienceSubmitDTO));
    }
}
